<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-platform">&nbsp;&nbsp;生产管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-circle-plus">&nbsp;&nbsp;设备管理添加</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div>
      <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
        <el-divider content-position="left">数据添加</el-divider>
        <el-form
            :model="equipmentForm"
            :rules="rules"
            ref="equipmentForm"
            label-width="100px"
            style="margin-right: 25px">
          <el-row>
            <el-col :span="8">
              <el-form-item label="设备ID:" prop="equipmentId">
                <el-input v-model="equipmentForm.equipmentId" placeholder="请输入设备ID"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="设备名称:" prop="equipmentName">
                <el-input v-model="equipmentForm.equipmentName" placeholder="输入设备名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="生产单位:" prop="model">
                <el-input v-model="equipmentForm.model" placeholder="输入生产单位"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="生产日期:" prop="produceDate">
                <el-date-picker
                    v-model="equipmentForm.produceDate"
                    type="date"
                    format="yyyy 年 MM 月 dd 日"
                    value-format="yyyy-MM-dd"
                    placeholder="选择生产日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="启用日期:" prop="enabledDate">
                <el-date-picker
                    style="width: 80%"
                    v-model="equipmentForm.enabledDate"
                    type="date"
                    format="yyyy 年 MM 月 dd 日"
                    value-format="yyyy-MM-dd"
                    placeholder="选择启用日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="安装单位:" prop="installationUnit">
            <el-input v-model="equipmentForm.installationUnit" placeholder="输入安装单位"></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="上检时间:" prop="lastDetectedTime">
                <el-date-picker
                    v-model="equipmentForm.lastDetectedTime"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    placeholder="选择上次检测时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="负责人:" prop="head">
                <el-input v-model="equipmentForm.head" placeholder="输入负责人"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="异常情况:" prop="abnormal">
            <el-input v-model="equipmentForm.abnormal" placeholder="输入异常情况"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('equipmentForm')" style="color: white">添加</el-button>
            <el-button type="warning" @click="resetForm('equipmentForm')" style="color: white">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      equipmentForm: {
        equipmentId: 12,
        equipmentName: '联想电脑',
        model: '台',
        produceDate: '2022-10-12',
        installationUnit: '联想售后中心',
        enabledDate: '2022-10-20',
        lastDetectedTime: '2023-03-15 12:00:00',
        abnormal: '无异常,可正常使用',
        head: '于立杰',
      },
      rules: {
        equipmentId: [
          {required: true, message: "请输入设备ID", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        equipmentName: [
          {required: true, message: "请输入设备名称", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        model: [
          {required: true, message: "请输入生产单位", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        produceDate: [
          {required: true, message: "请选择生产日期", trigger: 'change'}
        ],
        installationUnit: [
          {required: true, message: "请输入安装单位", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        enabledDate: [
          {required: true, message: "请选择启用日期", trigger: 'change'}
        ],
        lastDetectedTime: [
          {required: true, message: "请选择上检时间", trigger: 'change'}
        ],
        abnormal: [
          {required: true, message: "请输入异常情况", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        head: [
          {required: true, message: "请输入负责人", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/produceEquipments/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.equipmentForm);
          console.log('formData = ' + formData);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log(responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}

</script>


<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

</style>